package com.ers.dao.impl;

import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import com.ers.dao.ResumeDao;
import com.ers.model.EducationInfo;
import com.ers.model.Intention;
import com.ers.model.ProjectInfo;
import com.ers.model.Resume;
import com.ers.model.SkillInfo;
import com.ers.model.User;
import com.ers.model.UserInfo;
import com.ers.validmodel.ResumeSearchModel;

public class ResumeDaoImpl implements ResumeDao{
	private SessionFactory sessionFactory;	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	public Session getSession(){
		return sessionFactory.getCurrentSession();
	}
	@Override
	public void saveInfo(UserInfo userInfo) {	
		getSession().saveOrUpdate(userInfo);
		sessionFactory.getCurrentSession().flush();
		sessionFactory.evict(UserInfo.class);
	}
	@Override
	public List<Resume> loadResumeByUser(int id) {
		List<Resume> r = (List<Resume>)sessionFactory.getCurrentSession().createCriteria(Resume.class).add(Restrictions.eq("user.id", id)).list();
		return r;
	}
	@Override
	public Resume loadResumeById(int id) {
		return (Resume)sessionFactory.getCurrentSession().load(Resume.class, id);
	}
	@Override
	public void saveIntention(Intention intention) {
		getSession().saveOrUpdate(intention);
	}
	@Override
	public void saveEducation(EducationInfo education) {
		getSession().saveOrUpdate(education);
	}
	@Override
	public void saveProject(ProjectInfo projectInfo) {
		getSession().saveOrUpdate(projectInfo);
	}
	@Override
	public List<SkillInfo> loadSkill() {
		List<SkillInfo> skillInfos = null;
		skillInfos = (List<SkillInfo>)getSession().createCriteria(SkillInfo.class).list();
		return skillInfos;
	}
	@Override
	public void saveSkill(int id, int resumeId) {
		SkillInfo skill = (SkillInfo)getSession().load(SkillInfo.class, id);
		Resume resume = (Resume)getSession().load(Resume.class, resumeId);
		Set<SkillInfo> skills = resume.getSkillInfo();
		skills.add(skill);
		resume.setSkillInfo(skills);
		getSession().saveOrUpdate(resume);	
	}
	@Override
	public List<Object[]> findBySearcher(ResumeSearchModel rsm,int pageSize,int pageNo) {
		StringBuilder sb = new StringBuilder();
		sb.append("select r.id,u.name,u.sex,e.education,e.major,u.experience,i.workArea,i.position from Resume r,UserInfo u,EducationInfo e,Intention i where r.userInfo.id=u.id and r.intention.id = i.id and r.id=e.resume.id and 1=1");
		if(rsm.getPosition()!=null&&!rsm.getPosition().equals(""))
			sb.append(" and i.position like '%"+rsm.getPosition()+"%'");
		if(rsm.getEducation()!=null && !rsm.getEducation().equals(""))
			sb.append(" and e.education='"+rsm.getEducation()+"'");
		if(rsm.getExperience()!=null&!rsm.getExperience().equals(""))
			sb.append(" and u.experience='"+rsm.getExperience()+"'");
		if(rsm.getMajor()!=null && !rsm.getMajor().equals(""))
			sb.append(" and e.major='"+rsm.getMajor()+"'");
		if(rsm.getUniversity()!=null &&!rsm.getUniversity().equals(""))
			sb.append(" and e.university='"+rsm.getUniversity()+"'");
		if(rsm.getWorkArea()!=null && !rsm.getWorkArea().equals(""))
			sb.append(" and i.workArea='"+"'");
		if(rsm.getLowsalary()!=0 && rsm.getLowsalary()< rsm.getHighsalary())
			sb.append(" and i.salary>"+rsm.getLowsalary());
		if(rsm.getLowsalary()!=0 && rsm.getLowsalary()< rsm.getHighsalary())
			sb.append(" and i.salary<"+rsm.getHighsalary());
		String hql = sb.toString();
		List<Object[]> os = (List<Object[]>)sessionFactory.getCurrentSession().createQuery(hql).setFirstResult((pageNo-1)*pageSize).setMaxResults(pageSize).list();
		return os;
	}
	@Override
	public List<Object[]> findByCompanyId(int id,int pageSize,int pageNo) {
		String hql = "select r.id,r.userInfo.name,r.userInfo.sex,r.userInfo.experience,r.intention.workArea,r.intention.position,re.id from Resume r,Record re where r.id=re.resume.id and re.company.id=:id";
		List<Object[]> os = (List<Object[]> )sessionFactory.getCurrentSession().createQuery(hql).setInteger("id", id)
				.setFirstResult((pageNo-1)*pageSize).setMaxResults(pageSize).list();
		return os;
	}
	@Override
	public boolean save(int userId) {
		User u = new User();
		u.setId(userId);
		Resume r = new Resume();
		r.setUser(u);
		sessionFactory.getCurrentSession().save(r);
		return true;
	}
	

}
